home *** CD-ROM | disk | FTP | other *** search
- #include <strings.h>
- #include <storage.h>
-
- #include <QuickDraw.h>
- #include <MacTypes.h>
- #include <FontMgr.h>
- #include <WindowMgr.h>
- #include <MenuMgr.h>
- #include <TextEdit.h>
- #include <DialogMgr.h>
- #include <EventMgr.h>
- #include <DeskMgr.h>
- #include <StdfilePkg.h>
- #include <FileMgr.h>
- #include <ToolboxUtil.h>
- #include <ControlMgr.h>
-
- #include "Harmonic.h"
-
-
- /************* globals ***************/
-
- int errno; /* required by the math lib */
-
- /************** static globals *****************/
-
- char gCR[] = "\r";
- char gTAB[] = "\t";
- char gSP[] = " "; /* space */
- char gEOL[] = "\0";
-
-
-
-
- /* Simple Harmonic motion simulator application.
- The following are included into the project:
-
- mactraps lib
- math lib
- storage lib
- strings lib
- harmonic sim.c
- harmonic functs.c
- plot.c
- utils.c
-
-
- Written by : Byro
- Date : 03/05/90
- Modified : 04/27/90
-
-
- Version Log:
-
- */
-
-
- main()
-
- {
-
- Boolean doneFlag = FALSE, WNEImplemented;
- EventRecord myEvent;
- Handle ClipHandle;
- MenuHandle AppleMenu;
- Rect DragRect;
- WindowPtr PlotWindow,
- ClipboardWindow,
- StatusWindow;
-
- ToolBoxInit();
- WindowInit(&PlotWindow, "\pPlot Window");
- WindowInit(&StatusWindow, "\pStatus Window");
- SetUpDragRect(&DragRect);
- MenuBarInit(&AppleMenu);
- CreateClipBoardWindow(&ClipboardWindow);
- ClipHandle = NewHandle(NIL);
- MainLoop(&doneFlag, &myEvent, &WNEImplemented, &DragRect, &AppleMenu,
- &PlotWindow, &ClipboardWindow, &StatusWindow, &ClipHandle);
-
- }
-
-
-
-
-
- ToolBoxInit()
-
- /* initialization */
-
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NIL);
- InitCursor();
- }
-
-
-
-
- WindowInit(AppWindow, windowTitle)
- WindowPtr *AppWindow;
- Str255 *windowTitle;
-
- {
- /* init window ptr */
-
- *AppWindow = GetNewWindow(BASE_RES_ID, NIL, MOVE_TO_FRONT);
-
- /* if can't open window let user know */
-
- if (*AppWindow == NIL)
- {
- /* system dialog goes here */
-
- ExitToShell(); /* caio */
- }
-
- if (**windowTitle != NIL)
- SetWTitle(*AppWindow, windowTitle);
-
- SetPort(*AppWindow); /* make current graf port */
-
- }
-
-
-
-
- SetUpDragRect(DragRect)
- Rect *DragRect;
-
- {
- *DragRect = screenBits.bounds;
- (*DragRect).left += DRAG_THRESHOLD;
- (*DragRect).right -= DRAG_THRESHOLD;
- (*DragRect).bottom -= DRAG_THRESHOLD;
- }
-
-
-
-
- MenuBarInit(AppleMenu)
- MenuHandle *AppleMenu;
-
- {
- Handle myMenuBar;
-
- myMenuBar = GetNewMBar(BASE_RES_ID);
- SetMenuBar(myMenuBar);
- *AppleMenu = GetMHandle(APPLE_MENU_ID);
- AddResMenu(*AppleMenu, 'DRVR');
- DrawMenuBar();
- }
-
-
-
-
- CreateClipBoardWindow(ClipBoardWindow)
- WindowPtr *ClipBoardWindow;
-
- {
-
- if (( *ClipBoardWindow = GetNewWindow(BASE_RES_ID, NIL, MOVE_TO_FRONT)) == NIL)
- {
- NoteAlert(CLIPBOARD_ALERT, NIL);
- /* ExitToShell(); */
- }
-
- /* set window title */
-
- SetWTitle(*ClipBoardWindow, "\p Clip Board");
-
- MoveWindow(*ClipBoardWindow, WINDOW_HOME_LEFT, WINDOW_HOME_RIGHT,
- LEAVE_WHERE_IT_IS);
- }
-
-
-
-
-
- MainLoop(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu,
- PlotWindow, CBWindow, StatWindow, clipHandle)
- Boolean *doneFlag;
- EventRecord *myEvent;
- Boolean *WNEImplemented;
- Rect *DragRect;
- MenuHandle *AppleMenu;
- WindowPtr *PlotWindow,
- *CBWindow,
- *StatWindow;
- Handle *clipHandle;
-
- {
-
- /* check to see if multifinder running */
-
- *WNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
- NGetTrapAddress(UNIMPL_TRAP_NUM, ToolTrap));
-
- /* main program/Event loop */
-
- do
- {
- HandleEvent(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu,
- PlotWindow, CBWindow, StatWindow, clipHandle);
- }
- while (*doneFlag == FALSE);
- }
-
-
-
-
- HandleEvent(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu,
- PlotWindow, CbWindow, StWindow, clipHandl)
- Boolean *doneFlag;
- EventRecord *myEvent;
- Boolean *WNEImplemented;
- Rect *DragRect;
- MenuHandle *AppleMenu;
- WindowPtr *PlotWindow,
- *CbWindow,
- *StWindow;
- Handle *clipHandl;
-
- {
- char theChar;
- int Err = noErr;
- MenuHandle itemHndl;
-
- static float *xptr, *yptr; /* ptrs used to alloc/dealloc space */
- static Boolean plotFile = FALSE;
-
- CursHandle theCursor; /* changes cursor to a watch during update */
-
- /* multifinder check */
-
- if (*WNEImplemented)
- WaitNextEvent(everyEvent, myEvent, SLEEP, NIL_MOUSE_REGION);
- else
- {
- SystemTask(); /* time slice for OS */
- GetNextEvent(everyEvent, myEvent);
- }
-
- switch ((*myEvent).what)
- {
- case mouseDown:
- HandleMouseDown(doneFlag, myEvent,WNEImplemented, DragRect, AppleMenu,
- &plotFile, PlotWindow, CbWindow, StWindow);
- break;
-
- case nullEvent:
- case mouseUp:
- case keyUp:
- case updateEvt:
- /*
- see if update is for this window or open file menu selected
- & redraw window if either condition true
- */
-
- if (((WindowPtr) (*myEvent).message == *PlotWindow) || (plotFile == TRUE))
- {
- theCursor = GetCursor(WATCH_ICON); /* draw watch while waiting */
- SetCursor(*theCursor);
-
- BeginUpdate(*PlotWindow);
-
- /* main plotting routine */
-
- SetPort(*PlotWindow);
- Err = MainPlot(PlotWindow, &plotFile, &xptr, &yptr);
- EndUpdate(*PlotWindow);
-
- InitCursor(); /* restore cursor to default icon */
- plotFile = FALSE; /* reset after use */
- if (Err == CANCEL_BUTTON)
- {
- /* reenable menu selection if cancel button hit */
-
- itemHndl = GetMHandle(OPTION_MENU_ID);
- EnableItem(itemHndl, PLOT_ITEM);
- break; /* get out of event loop now! */
- }
- }
-
- /* chk if update for clip board window */
-
- if ((WindowPtr) (*myEvent).message == *CbWindow)
- {
- BeginUpdate(*CbWindow);
- SetPort(*CbWindow);
- DisplayClipboard(CbWindow, clipHandl);
- ShowWindow(*CbWindow);
- EndUpdate(*CbWindow);
- }
-
- /* chk if update for status window */
-
- if ((WindowPtr) (*myEvent).message == *StWindow)
- {
- BeginUpdate(*StWindow);
- SetPort(*StWindow);
- if ((doStatus((double) NIL, (double) NIL,
- (double) NIL, "\p", StWindow)) == TRUE)
- {
- ShowWindow(*StWindow);
- }
- EndUpdate(*StWindow);
- }
- break;
- case keyDown:
- case autoKey:
- theChar = (*myEvent).message & charCodeMask;
- if (((*myEvent).modifiers & cmdKey) != 0)
- HandleMenuChoice(MenuKey(theChar), doneFlag, AppleMenu, &plotFile,
- CbWindow, StWindow, PlotWindow);
- break;
- case activateEvt:
- break;
- default:
- break;
- }
-
- if (*doneFlag == TRUE)
- {
- if (xptr != NIL) /* release space if no errors */
- {
- if ((free(xptr)) == MinusOne)
- HandleError(FREE_MEM_ALERT); /* error dialog */
- }
-
- if (yptr != NIL)
- {
- if ((free(yptr)) == MinusOne)
- HandleError(FREE_MEM_ALERT); /* error dialog */
- }
- }
- }
-
-
-
-
- HandleMouseDown(doneFlag, myEvent, WNEImplemented, DragRect, AppleMenu,
- plotFilePtr, plotWindow, cBwindow, sWindow)
- Boolean *doneFlag;
- EventRecord *myEvent;
- Boolean *WNEImplemented;
- Rect *DragRect;
- MenuHandle *AppleMenu;
- Boolean *plotFilePtr;
- WindowPtr *plotWindow,
- *cBwindow,
- *sWindow;
-
- {
- Boolean StillInGoAway;
- WindowPtr whichWindow;
- short int thePart;
- long int menuChoice;
- MenuHandle disableHandle;
- static WindowPtr oldPort;
-
- thePart = FindWindow((*myEvent).where, &whichWindow);
- switch(thePart)
- {
- case inContent:
- /* activate the window that was pt'd @ */
-
- GetPort(&oldPort); /* save current graph port */
- SetPort(whichWindow);
- SelectWindow(whichWindow);
- break;
- case inMenuBar:
- menuChoice = MenuSelect((*myEvent).where);
- HandleMenuChoice(menuChoice, doneFlag, AppleMenu, plotFilePtr,
- cBwindow, sWindow, plotWindow);
- break;
- case inSysWindow:
- SystemClick(myEvent, whichWindow);
- break;
- case inDrag:
- DragWindow(whichWindow, (*myEvent).where, DragRect);
- break;
- case inGoAway:
- /* highlight close box when clicked */
-
- StillInGoAway = TrackGoAway(whichWindow, (*myEvent).where);
- if (StillInGoAway)
- {
- /* re-enable menu selection when window closed */
-
- if (whichWindow == *plotWindow)
- {
- disableHandle = GetMHandle(OPTION_MENU_ID);
- EnableItem(disableHandle, PLOT_ITEM);
- }
-
- if (whichWindow == *cBwindow)
- {
- disableHandle = GetMHandle(EDIT_MENU_ID);
- EnableItem(disableHandle, CLIPBOARD_ITEM);
- }
-
- if (whichWindow == *sWindow)
- {
- disableHandle = GetMHandle(OPTION_MENU_ID);
- EnableItem(disableHandle, STATUS_ITEM);
- }
-
- /* restore old graph port */
-
- SetPort(oldPort);
- HideWindow(whichWindow);
- SendBehind(whichWindow, NIL);
- }
- break;
- default:
- break;
- }
- }
-
-
-
-
- HandleMenuChoice(menuChoice, doneFlag, AppleMenu, plotFptr, cbWindow,
- stWindow, pltWindow)
- long menuChoice;
- Boolean *doneFlag;
- MenuHandle *AppleMenu;
- Boolean *plotFptr;
- WindowPtr *cbWindow,
- *stWindow,
- *pltWindow;
-
- {
- int theMenu;
- int theItem;
-
- if (menuChoice != FALSE)
- {
- theMenu = HiWord(menuChoice);
- theItem = LoWord(menuChoice);
- switch (theMenu)
- {
- case APPLE_MENU_ID:
- HandleAppleChoice(theItem, AppleMenu);
- break;
- case EDIT_MENU_ID:
- HandleEditChoice(theItem, cbWindow, pltWindow);
- break;
- case FILE_MENU_ID:
- HandleFileChoice(theItem, doneFlag);
- break;
- case OPTION_MENU_ID:
- HandleOptionChoice(theItem, plotFptr, stWindow);
- break;
- default:
- break;
- }
- HiliteMenu(0);
- }
- }
-
-
-
-
-
- HandleAppleChoice(theItem, AppleMenu)
- int theItem;
- MenuHandle *AppleMenu;
-
- {
- Str255 accName;
- int accNumber;
- short int itemNumber;
- DialogPtr AboutDialog;
-
- switch (theItem)
- {
- case ABOUT_ITEM:
- NoteAlert(ABOUT_ALERT, NIL);
- break;
- default:
- GetItem(*AppleMenu, theItem, accName);
- accNumber = OpenDeskAcc(accName);
- break;
- }
- }
-
-
-
-
- HandleEditChoice(theItem, theCbWindow, thePlotWindow)
- int theItem;
- WindowPtr *theCbWindow,
- *thePlotWindow;
-
- {
- long Err, *myPtr, ptr = NIL;
- MenuHandle editHandle;
- PScrapStuff myScrapPtr;
- Str255 myEmptyStr;
-
- myPtr = (long *) gSP;
-
- /* generate update evnt for clipboard window */
-
- switch (theItem)
- {
- case COPY_ITEM:
- /* defined by application */
- break;
- case PASTE_ITEM:
- /* defined by application */
- break;
- case CLEAR_ITEM:
- /* clear clipboard; refer to I.M. I:385-386, I:461 */
-
- myScrapPtr = InfoScrap();
- Err = ZeroScrap();
- Err = PutScrap(1, 'PAT ', myPtr);
- SetPort(*theCbWindow);
- EraseRect(&(*theCbWindow)->portRect);
- UnloadScrap(); /* put it on disk clipboard */
- break;
- case CLIPBOARD_ITEM:
- /* disable item when selected */
-
- editHandle = GetMHandle(EDIT_MENU_ID);
- DisableItem(editHandle, CLIPBOARD_ITEM);
-
- SetPort(*theCbWindow);
- SelectWindow(*theCbWindow);
- ShowWindow(*theCbWindow);
- break;
- default:
- break;
- }
- }
-
-
-
-
- HandleFileChoice(theItem, doneFlag)
- int theItem;
- Boolean *doneFlag;
-
- {
- OSErr DoLaunch(); /* forward declaration */
- Boolean SubLaunch; /* sublaunch if true and launch if false */
- int Err = noErr;
- MenuHandle fileHandle;
-
- switch (theItem)
- {
- case TRANSFER_ITEM:
- /* quits current routine after launch */
-
- SubLaunch = FALSE;
- Err = DoLaunch(SubLaunch);
- break;
- case QUIT_ITEM:
- *doneFlag = TRUE;
- break;
- }
-
- /* handle launch failure */
-
- if (Err != noErr)
- NoteAlert(LAUNCH_ALERT, NIL);
- }
-
-
-
-
- HandleOptionChoice(Item, PlotFPtr, sWindow)
- int Item;
- Boolean *PlotFPtr;
- WindowPtr *sWindow;
-
- {
- CursHandle theCursor;
- double atof(),
- Coeff = (double) NIL,
- Mass = (double) NIL,
- Elast = (double) NIL;
- MenuHandle OptionHndl;
- SavedSettings getSettings,
- Settings;
- SavedSettingsPtr SettingsPtr;
-
- SettingsPtr = &getSettings;
-
- switch (Item)
- {
- case SIMULATE_ITEM:
- /* get input from dialog. Convert strings to floats and
- pass into calc harmonics if the structure ret'd from
- handle dialog is valid */
-
- if (HandleDialog(&SettingsPtr) == TRUE)
- {
- theCursor = GetCursor(WATCH_ICON); /* wait cursor */
- SetCursor(*theCursor);
-
- PtoC(&getSettings, &Settings);
- Coeff = atof(Settings.amp);
- Mass = atof(Settings.mass);
- Elast = atof(Settings.elastic);
- CalcHarmonic(Coeff, atof(Settings.frict), Elast, Mass,
- (int) atof(Settings.start), (int) atof(Settings.fini),
- (int) atof(Settings.ink), Settings.xVal, Settings.yVal);
- doStatus(Coeff, Mass, Elast, Settings.simName, sWindow);
- }
- InitCursor(); /* reset to default cursor */
- break;
- case PLOT_ITEM:
- OptionHndl = GetMHandle(OPTION_MENU_ID);
- DisableItem(OptionHndl, PLOT_ITEM);
- *PlotFPtr = TRUE;
- break;
- case STATUS_ITEM:
- /* disable menu selection */
-
- OptionHndl = GetMHandle(OPTION_MENU_ID);
- DisableItem(OptionHndl, STATUS_ITEM);
-
- /* display status window */
-
- SetPort(*sWindow);
- if ((doStatus(Coeff, Mass, Elast, "\p", sWindow)) == TRUE)
- {
- SelectWindow(*sWindow);
- ShowWindow(*sWindow);
- }
- else
- {
- OptionHndl = GetMHandle(OPTION_MENU_ID);
- EnableItem(OptionHndl, STATUS_ITEM);
- }
- break;
- default:
- break;
- }
- }
-
-
-
-
- DisplayClipboard(BoardWindow, ClipHandle)
- WindowPtr *BoardWindow;
- Handle *ClipHandle;
-
- /* comment later */
-
- {
- Rect myRect;
- long length,
- offset;
-
- if ((length = GetScrap(*ClipHandle, 'TEXT', &offset)) < 0)
- {
- if ((length = GetScrap(*ClipHandle, 'PICT', &offset)) < 0)
- ;/* NoteAlert(CLIPBOARD_ALERT, NIL); */
- else
- {
- /* display pict data */
-
- myRect = (*BoardWindow)->portRect;
- CenterPict(*ClipHandle, &myRect);
- DrawPicture(*ClipHandle, &myRect);
- }
- }
- else
- {
- /* display text data */
-
- HLock(*ClipHandle);
- TextBox(**ClipHandle, length, &(thePort->portRect), teJustLeft);
- HUnlock(*ClipHandle);
- }
- }
-
-
-
-
-
- OSErr LaunchIt(pLnch)
- pLaunchStruct pLnch;
-
- /*
- This function is pascal call back routine. It lanches nu apps; refer
- to pp. 118 & 119 in the ls c manual for more info.
- note: result code < 0 means error
- */
-
- {
- asm
- {
- move.l 8(sp),a0 /* pops ptr into a0 */
- _Launch /* calls launch */
- /* OSErr ret'd in d0 */
- }
- }
-
-
-
-
- OSErr DoLaunch(subLaunch)
- Boolean subLaunch; /* sublaunch if true & launch if false */
-
- {
- LaunchStruct myLaunch;
- Point where; /* where to display dialog */
- SFReply reply; /* reply record */
- SFTypeList myFileTypes; /* we only want appls */
- short int numFileTypes = 1;
- HFileInfo myPB;
- StringPtr dirNameStr;
- OSErr err;
- OSErr LaunchIt(); /* forward declare funct */
-
- where.h = 80;
- where.v = 90;
- myFileTypes[0] = 'APPL'; /* only appls */
-
- /* let the user choose the file to launch */
-
- SFGetFile(where, "", NIL, numFileTypes, myFileTypes, NIL, &reply);
-
- if (reply.good)
- {
- dirNameStr = (StringPtr) &reply.fName; /* init to file selected */
-
- /* get finder flags */
-
- myPB.ioCompletion = NIL;
- myPB.ioNamePtr = dirNameStr;
- myPB.ioVRefNum = reply.vRefNum;
- myPB.ioFDirIndex = 0;
- myPB.ioDirID = 0;
- err = PBGetCatInfo((CInfoPBPtr) &myPB, FALSE);
- if (err != noErr)
- return(err); /* how to handle this? */
-
- /* set the current volume to where the target appl is */
-
- err = SetVol(NIL, reply.vRefNum);
- if (err != noErr)
- return(err); /* ditto */
-
- /* set up the launch params */
-
- myLaunch.pfName = (StringPtr) &reply.fName; /* ptr to our filename */
- myLaunch.param = 0; /* we don't want alt screen or sound buffs */
-
- /* set up LC so as to tell launch that htere is non-junk next */
-
- myLaunch.LC[0] = 'L'; myLaunch.LC[1] = 'C';
- myLaunch.extBlockLen = 6; /* len of param blk past this long word */
-
- /* copy flags; set bit 6 of low byte to 1 for RO access: */
-
- myLaunch.fFlags = myPB.ioFlFndrInfo.fdFlags; /* from _GetCatInfo */
-
- /* test sublaunch & set launch flags accordingly */
-
- if (subLaunch)
- myLaunch.launchFlags = 0xC0000000; /* set 2 high bit for sublaunch */
- else
- myLaunch.launchFlags = 0x00000000; /* launch & quit */
- /* Debugger(); */
- err = LaunchIt(&myLaunch); /* call _Launch */
- if (err < 0)
- {
- /* launch failed, put up alert to tell user */
- /* LaunchFailed(); */
- return(err);
- }
- else
- return(noErr);
- }
- }
-
-
-
-
- CenterPict(thePicture, myRectPtr)
- PicHandle thePicture;
- Rect *myRectPtr;
-
- {
- Rect windRect,
- pictureRect;
-
- windRect = *myRectPtr;
- pictureRect = (**(thePicture)).picFrame;
- myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom
- - pictureRect.top)) / 2 + windRect.top;
- myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
- myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right
- - pictureRect.left)) / 2 + windRect.left;
- myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
- }
-
-